﻿using ESRI.ArcGIS.Carto;

namespace DllShepherd.Core.Esri.Server
{
    public class MsdUtils
    {
        private const bool UseRelativePath = true;
        private const bool ValidateLayerConnection = false;

        /// <summary>
        /// Changes the SDE connection string for all the layers in the MSD file
        /// </summary>
        /// <param name="msdFilePath"></param>
        /// <param name="connectionString"></param>
        /// <param name="resultMsdFile">result file is relative path</param>
        public static void ChangeConnection(string msdFilePath, string connectionString, string resultMsdFile)
        {
            IMSDHelper helper = new MSDHelper();
            helper.Open(msdFilePath);
            var maps = helper.GetMaps();
            for (var i = 0; i < maps.Count; i++)
            {
                var layers = helper.GetLayers(maps.Element[i]);
                for (var j = 0; j < layers.Count; j++)
                {
                    helper.PutWorkspaceConnectionStringInLayer(layers.Element[j], connectionString, ValidateLayerConnection);
                }
            }
            helper.SaveAs(resultMsdFile, UseRelativePath);
        }
    }
}
